<?php
class Student extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('score_model');
		$this->load->model('student_model');
		$this->load->model('verify_model');
		$this->load->helper('url_helper');
		$this->load->library('session');
		$this->load->library('form_validation');
	}

	public function index()
	{
		$this->load->view('public/head');
		$this->load->view('student/index');
		$this->load->view('public/foot');
	}

	public function create()
	{	
		if(!isset($_SESSION['user']))
		{
			$this->load->view('public/head');
			$this->load->view('student/index');
			$this->load->view('public/foot');
			return 0;
		}

		$json_display = file_get_contents('resource/js/display.json');
		$detail_array = json_decode($json_display,true);		
		$verify_array = $this->score_model->find($_SESSION['user']);
		$student = $this->student_model->read_student($_SESSION['user']);

		$data['detail']= $detail_array;
		$data['student'] = $student;
		if(!empty($verify_array))
		{				
            $data['score'] = $verify_array;
			$this->load->view('public/head');
			$this->load->view('student/show_verify_view',$data);
			$this->load->view('public/foot');
			return 0;
		}

		$test_json = file_get_contents('resource/js/test.json');
		// 把JSON字符串转成PHP数组
		$test_array = json_decode($test_json, true);
		if(isset($_POST['parm1_1'][0]))
		{
			$save['id'] = $_SESSION['user'];
			$this->score_model->delete($save['id']);
			foreach($test_array as $score_item)
			{
				$save['description'] = $score_item['name'];
				$save['parm_num'] = $score_item['parm_num'];
				$save['rule_id'] = $score_item['id'];
				$save['category'] = $score_item['category'];
				$count = count($_POST['parm1_'.$score_item['id']]);
				for($j=0;$j<$count;$j++)
				{
					$parm1 = $this->input->post('parm1_'.$score_item['id'].'['.$j.']');
					$parm1 = explode("_",$parm1)[0];
					if($parm1 =='none')
					{	
						continue;
					}
					//保存上传文件
					move_uploaded_file($_FILES[$score_item['id']]['tmp_name'][$j],"upload/" . $_FILES[$score_item['id']]["name"][$j]);
					for($i=1;$i<=$score_item['parm_num'];$i++)
					{
						$parm =$this->input->post('parm'.$i.'_'.$score_item['id'].'['.$j.']');
						$save['parm'.$i] = $parm;
					}
					$save['file'] = $_FILES[$score_item['id']]["name"][$j];
					$save['remark'] = $this->input->post('remark_'.$score_item['id'].'['.$j.']');
					$score_rule = $score_item['rule'];
					for($k=1;$k<=$score_item['parm_num'];$k++)
					{
						$score_rule = $score_rule[$save['parm'.$k]];
					}
					$save['score'] = $score_rule;
					$save['record_id'] = $save['id'].'_'.$save['rule_id'].'_'.$j;
					$this->score_model->write_score($save);
				}
			}			
		}

		$this->form_validation->set_rules("modify","Modify","required");
		$score = $this->score_model->read_score($_SESSION['user']);
		if(!empty($score)&&($this->form_validation->run()===false))
		{
			$data['score'] = $score;
			$this->load->view('public/head');
			$this->load->view('student/show_score_view',$data);
			$this->load->view('public/foot');
			return 0;
		}

		$show_data['rule'] = $test_array;
		$this->load->library('form_validation');
		$this->load->view('public/head');
		$this->load->view('student/create_view',$show_data);
		$this->load->view('public/foot');
		// else
		// {
		// 	$student = $this->student_model->read_student($_SESSION['user']);
		// 	$data['detail']= $detail_array;
		// 	$data['score'] = $score;
		// 	$data['student'] = $student;
		// 	$this->load->view('public/head');
		// 	$this->load->view('student/succeed',$data);
		// 	$this->load->view('public/foot');
			
		// }
	}
//写入审批记录
	public function result(){
		// if(!isset($_POST['record_id']))
		// {
		// 	$result_array['id'] = $_POST['id'];
		// 	$result_array['status'] = '已审核';
		// 	$this->score_model->write_result($result_array);
		// 	header("Location: http://[::1]/score/index.php/student/verify");
		// }

		if(isset($_POST['record_id']))
		{
			$i = 0;
			//保存审批记录
			foreach ($_POST['record_id'] as $verify_item)
			{
				$i++;
				//$record_array = $this->score_model->read_score_by_id($verify_item);
				$record_array['verify_result'] = $_POST['verify_'.$i];
				$record_array['record_id'] = $verify_item;
				$this->score_model->undate_result($record_array);
			}
		}
		
		$student_array = $this->student_model->read_student($_POST['id']);
		//计算总得分，并存入数据库
		$moral = $student_array['moral'];
		$intelligence = $student_array['intelligence'];
		$sports = $student_array['sports'];

		if(isset($_POST['record_id']))
		{
			$relation['id'] = $_POST['id'];
			$relation['verify_result'] ='yes';
			$relation['category'] ='德育';
			$moral_score = $this->score_model->sum($relation);
			$moral = $moral +$moral_score['score'];
			$moral = min($moral,120);
			$relation['category'] ='智育';
			$intelligence_score = $this->score_model->sum($relation);
			$intelligence = $intelligence +$intelligence_score['score'];
			$intelligence = min($intelligence,120);
			$relation['category'] ='德育';
			$sports_score = $this->score_model->sum($relation);
			$sports = $sports +$sports_score['score'];
			$sports= min($sports,120);
		}
		$sum = $moral*0.25+$intelligence*0.65+$sports*0.1;

		$j = 0;
		
		$result_array['id'] = $_POST['id'];
		$result_array['name']= $student_array['name'];
		$result_array['status'] = '已审核';
		$result_array['total_score'] = $sum;
		$this->score_model->write_result($result_array);
		$data['sum'] = $sum;
		header("Location: http://[::1]/score/index.php/student/verify"); 
		$this->load->view('public/head');
		$this->load->view('student/result_view',$data);
		$this->load->view('public/foot');
	}
//教师输入学号审批
	public function verify($id="")
	{
		if(!isset($_SESSION['user']))
		{
			$this->load->view('public/head');
			$this->load->view('student/index');
			$this->load->view('public/foot');
			return 0;
		}
		//$this->form_validation->set_rules("student_id","Student_id","required");
		if($id =="")
		{	
			$result = $this->student_model->verify_result();
			$data['result'] = $result;
			$this->load->view('public/head');
			$this->load->view('student/verify_view',$data);
			$this->load->view('public/foot');
			return 0;
		}

		$json_display = file_get_contents('resource/js/display.json');
		$detail_array = json_decode($json_display,true);
		$score = $this->score_model->read_score($id);
		$student = $this->student_model->read_student($id);
		$data['detail']= $detail_array;
		$data['score'] = $score;
		$data['student'] = $student;
		$this->load->view('public/head');
		$this->load->view('student/succeed',$data);
		$this->load->view('public/foot');
	}

	public function login(){
		$id=$_POST['id'];
		$pwd=$_POST['pwd'];
		$query=$this->student_model->login($id,$pwd);
		if(empty($query))
			echo 0;
		else
		{
			$_SESSION['user']=$query['id'];
			echo 1;
		}			
	}

	public function logout(){
		unset($_SESSION['user']);
		$url= site_url('student/create');
		//Header('Location:http://[::1]/score/index.php/student/create'); 
		Header('Location:'.$url);
	}

	public function test()
	{	
		$score = $this->score_model->read_score($_SESSION['user']);
		$student = $this->student_model->read_student($_SESSION['user']);
		$detail_array = array('nation' =>'国家','province'=>'省级','city'=>'市级',
			'first'=>'一等奖','second'=>'二等奖','third'=>'三等奖','none'=>'未获奖');
		$data['detail']= $detail_array;
		$data['score'] = $score;
		$data['student'] = $student;
		$this->load->view('public/head');
		$this->load->view('student/succeed',$data);
		$this->load->view('public/foot');
	}
}
 ?>